package simple.entity

import console.Console
import groovy.xml.MarkupBuilder

class Action {

    Action() {
        library = new Library(name: 'Dunning');
        xmlParser = new XmlParser()
        initBooks()
        initBorrowers()
    }

    def readMenuSelection() {
        println()
        println('0:Quit')
        println('1:Add new Book')
        println('2:Display stock')
        println('3:Display books available for loan')
        println('4:Display books on loan')
        println('5:Register new borrower')
        println('6:Display borrowers')
        println('7:Lend one book')
        println('8:Return one book')
        println('9:remove one book')
        println('\n\tEnter choice>>>')
        return Console.readLine()
    }

    def addBook() {
        print('\nEnter book catalog number:')
        def catalogNumber = Console.readLine()
        print('\nEnter book title:')
        def title = Console.readLine()
        print('\nEnter book author:')
        def author = Console.readLine()
        print('\nEnter book publisher:')
        def publisher = Console.readLine()

        def bk = new Book(catalogNumber: catalogNumber, title: title, author: author, publisher: publisher)

        library.addBook(bk)
    }

    def removeBook() {
        print('\nEnter book catalog number:')
        def catalogNumber = Console.readLine()
        library.removeBook(catalogNumber)
    }

    def displayStock() {
        library.displayStock()
    }
    def displayBooksAvailableForLoan() {
        library.displayBooksAvailableForLoan()
    }
    def displayBooksOnLoan() {
        library.displayBooksOnLoan()
    }
    def displayBorrowers() {
        library.displayBorrowers()
    }
    def registerBorrower() {
        print('\nEnter borrower membership number:')
        def membershipNumber =Console.readLine()
        print('Enter borrower name:')
        def name =Console.readLine()

        def bor = new Borrower(membershipNumber: membershipNumber, name: name)
        library.registerBorrower(bor)
    }
    def lendBook() {
        print('\nEnter book catalog number:')
        def catalogNumber = Console.readLine()
        print('\nEnter borrower membership number:')
        def membershipNumber =Console.readLine()
        print('Enter borrower name:')
        library.lendBook(catalogNumber,membershipNumber)
    }
    def returnBook() {
        print('\nEnter book catalog number:')
        def catalogNumber = Console.readLine()
        library.returnBook(catalogNumber)
    }

    void storeBorrows() {
        def mB2 = new MarkupBuilder(new File('data/borrower.xml').newPrintWriter())

        def borrowers = mB2.borrowers() {
            library.borrowers.each { br ->
                mB2.borrower() {
                    membershipNumber(br.value.membershipNumber)
                    name(br.value.name)
                    if (br.value.borrowedBooks) {
                        br.value.borrowedBooks.each { bk ->
                            mB2.borrowedBooks() {
                                mB2.book() {
                                    title(bk.value.title)
                                    author(bk.value.author)
                                    publisher(bk.value.publisher)
                                    isbn(number: bk.key)
                                }
                            }
                        }
                    }
                }
            }
        }
        println 'borrowers is saved.'
    }

    void storeBooks() {
        // store data
        def mB = new MarkupBuilder(new File('data/library.xml').newPrintWriter())

        def lib = mB.library() {
            library.loanStock.each { bk ->
                mB.book() {
                    title(bk.value.title)
                    author(bk.value.author)
                    publisher(bk.value.publisher)
                    isbn(number: bk.key)
                }
            }
        }

        println 'library is saved.'
    }

    void initBorrowers() {
        def borrowDoc = xmlParser.parse('data/borrower.xml')

        //println "${borrowDoc.book[0].title[0].text()}"

        borrowDoc.borrower.each {
            br ->
                library.registerBorrower(new Borrower(membershipNumber: br.membershipNumber[0].text(), name: br.name[0].text()))
                br?.borrowedBooks?.book.each { bk ->
                    if (bk.isbn[0]['@number'] && br.membershipNumber[0].text()) {
                        library.lendBook(bk.isbn[0]['@number'], br.membershipNumber[0].text())
                    }
                }
        }
    }

    void initBooks() {
        //init library data
        def bookDoc = xmlParser.parse('data/library.xml')

        //println "${bookDoc.book[0].title[0].text()}"

        bookDoc?.book.each {
            bk ->
                library.addBook(new Book(catalogNumber: bk.isbn[0]['@number'], title: bk.title[0].text(), author: bk.author[0].text(), publisher: bk.publisher[0].text()))
        }
    }

    //----------properties----------
    def library
    def xmlParser
}